//
//  ZYAlertControl.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/4.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "ZYAlertControl.h"

@implementation ZYAlertControl

+ (void) showActionSheetWithTitle:(NSString *)title
                          message:(NSString *)msg
                      chooseBlock:(void (^)(NSInteger))block
                cancelButtonTitle:(NSString *)cancelString
           destructiveButtonTitle:(NSString *)destructiveButtonTitle
                 otherButtonTitle:(NSString *)otherButtonTitle, ...{
    
    NSMutableArray* argsArray = [[NSMutableArray alloc] initWithCapacity:3];
    
    
    if (cancelString) {
        [argsArray addObject:cancelString];
    }
    if (destructiveButtonTitle) {
        [argsArray addObject:destructiveButtonTitle];
    }
    
    id arg;
    va_list argList;
    if(otherButtonTitle)
    {
        [argsArray addObject:otherButtonTitle];
        va_start(argList,otherButtonTitle);
        while ((arg = va_arg(argList,id)))
        {
            [argsArray addObject:arg];
        }
        va_end(argList);
    }
    

        UIAlertController* alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleActionSheet];
        for (int i = 0; i < [argsArray count]; i++)
        {
            UIAlertActionStyle style =  (0 == i)? UIAlertActionStyleCancel: UIAlertActionStyleDefault;
            
            if (1==i && destructiveButtonTitle) {
                
                style = UIAlertActionStyleDestructive;
            }
            
            // 创建 actions.
            UIAlertAction *action = [UIAlertAction actionWithTitle:[argsArray objectAtIndex:i] style:style handler:^(UIAlertAction *action) {
                if (block) {
                    block(i);
                }
            }];
            [alertController addAction:action];
        }
        
        [[ZYAlertControl getCurrentViewController] presentViewController:alertController animated:YES completion:nil];
 
}

+ (void)showAlertWithTitle:(NSString *)title
                   message:(NSString *)msg
          statementButtons:(NSArray<NSString *> *)btnArray
               handleBlock:(void (^)(NSUInteger))block
         cancelButtonTitle:(NSString *)cancelString{
    
    UIAlertController* alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelString style:UIAlertActionStyleCancel handler:nil];
    
    [btnArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:[btnArray objectAtIndex:idx] style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            if (block) {
                block(idx);
            }
        }];
        [alertController addAction:action];
    }];
    [alertController addAction:cancelAction];

    [[ZYAlertControl getCurrentViewController] presentViewController:alertController animated:YES completion:nil];
}


+ (UIViewController*)getCurrentViewController
{
    UIViewController *result = nil;
    
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}

@end
